package AdminController.AdminMem;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.*;


import common.PagingManager;


public class AdminMem extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
    public AdminMem() {
        super();
      
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("*****회원맴버 관리모듈*****");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String action_code = request.getParameter("action_code");
		
		AdminBean adminmainbean[] = null;
		AdminMemDAO adminmaindao = new AdminMemDAO();
		
		RequestDispatcher view = null;
		PrintWriter out =response.getWriter();
		
			if(action_code.equals("younglist")){
				
				String search = null;
				String searchword = null;
				String vlaue = null;
				int count = 0;
		 		if(request.getParameter("search")!= null){
		 			search=request.getParameter("search");
				}
		 		
		 		int currentPage = 1;
		 		if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
					currentPage=Integer.parseInt(request.getParameter("currentPage"));
				}
		 		if(currentPage == 0){
		 			currentPage = 1;
		 		}
				
		 		HashMap param=new HashMap();
				param = new HashMap<String, Object>();
				param.clear();
					
				param.put("pageSize", 10);
				param.put("currentPage", currentPage );
				param.put("pageGroupSize", 10 );
				
				
				if(search == null){
					count = adminmaindao.memberCount(2);
					adminmainbean = adminmaindao.memberList(2, currentPage, 10);
				}else{
					if(search.equals("id")){
						searchword=request.getParameter("searchword");
						count = 1;
						adminmainbean = adminmaindao.memberList(searchword, currentPage, 10);
						vlaue = "text";
						
					}else if(search.equals("check")){
						vlaue=request.getParameter("checkvalue");
						
						count = adminmaindao.checkMemberCount(vlaue, "BABYPLUS", "Bby", 2);
						adminmainbean = adminmaindao.checkMemberList(2, "BABYPLUS", "Bby", vlaue, currentPage, 10);
						
					}else if(search.equals("mypage")){
						vlaue=request.getParameter("mypagevalue");
						if(vlaue.equals("yes")){
							vlaue = "true";
						}else{
							vlaue = "null";
						}
						
						count = adminmaindao.memberCount(vlaue, 2);
						adminmainbean = adminmaindao.mypageMemberList(2, vlaue, currentPage, 10);
						
					}else if(search.equals("all")){
						count = adminmaindao.memberCount(2);
						adminmainbean = adminmaindao.memberList(2, currentPage, 10);
						vlaue = "all";
					}
				}
				param.put("totalCount", count);

				PagingManager paging = new common.PagingManager();
				paging.setPageNo(param);

				for(int i=0; i<adminmainbean.length; i++){
					
						adminmainbean[i].setCheck(adminmaindao.checkGet("BABYPLUS", "Bby", adminmainbean[i].getID()));
				}
				
				System.out.println("부부회원목록 모으자!!");
				request.setAttribute("page","/Manage/Man_YoungPeople");
				request.setAttribute("adminmain", adminmainbean);
				request.setAttribute("search", search);
				request.setAttribute("value", vlaue);
				request.setAttribute("paging", paging.getReturnHtml());
				view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
				view.forward(request, response);
				
			}else if(action_code.equals("oldlist")){
				
				String search = null;
				String searchword = null;
				String vlaue = null;
				int count = 0;
		 		if(request.getParameter("search")!= null){
		 			search=request.getParameter("search");
				}
	
		 		int currentPage = 1;
		 		if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
					currentPage=Integer.parseInt(request.getParameter("currentPage"));
				}
		 		if(currentPage == 0){
		 			currentPage = 1;
		 		}
				
		 		HashMap param=new HashMap();
				param = new HashMap<String, Object>();
				param.clear();
					
				param.put("pageSize", 10);
				param.put("currentPage", currentPage );
				param.put("pageGroupSize", 10 );
				if(search == null){
					count = adminmaindao.memberCount(1);
					adminmainbean = adminmaindao.memberList(1, currentPage, 10);
				}else{
					if(search.equals("id")){
						searchword=request.getParameter("searchword");
						count = 1;
						adminmainbean = adminmaindao.memberList(searchword, currentPage, 10);
						vlaue = "text";
						
					}else if(search.equals("check")){
						vlaue=request.getParameter("checkvalue");
						
						count = adminmaindao.checkMemberCount(vlaue, "OLDPLUS", "Old", 1);
						adminmainbean = adminmaindao.checkMemberList(1, "OLDPLUS", "Old", vlaue, currentPage, 10);
						
					}else if(search.equals("mypage")){
						vlaue=request.getParameter("mypagevalue");
						if(vlaue.equals("yes")){
							vlaue = "true";
						}else{
							vlaue = "null";
						}
						
						count = adminmaindao.memberCount(vlaue, 1);
						adminmainbean = adminmaindao.mypageMemberList(1, vlaue, currentPage, 10);
						
					}else if(search.equals("all")){
						count = adminmaindao.memberCount(1);
						adminmainbean = adminmaindao.memberList(1, currentPage, 10);
						vlaue = "all";
					}
				}
				param.put("totalCount", count);
System.out.println("총 글수 = " + count);
				PagingManager paging = new common.PagingManager();
				paging.setPageNo(param);

				for(int i=0; i<adminmainbean.length; i++){
					
						adminmainbean[i].setCheck(adminmaindao.checkGet("OLDPLUS", "Old", adminmainbean[i].getID()));
				}
				
				System.out.println("늙은이회원목록 모으자!!");
				request.setAttribute("page","/Manage/Man_OldPeople");
				request.setAttribute("adminmain", adminmainbean);
				request.setAttribute("search", search);
				request.setAttribute("value", vlaue);
				request.setAttribute("paging", paging.getReturnHtml());
				view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
				view.forward(request, response);
				
				/*
		 		int currentPage = 1;
		 		if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
					currentPage=Integer.parseInt(request.getParameter("currentPage"));
				}
		 		if(currentPage == 0){
		 			currentPage = 1;
		 		}
				
		 		HashMap param=new HashMap();
				param = new HashMap<String, Object>();
				param.clear();
					
				param.put("pageSize", 10);
				param.put("currentPage", currentPage );
				param.put("pageGroupSize", 10 );
				int count = adminmaindao.memberCount(1);

				param.put("totalCount", count);

				PagingManager paging = new common.PagingManager();
				paging.setPageNo(param);
				
		 		adminmainbean = adminmaindao.memberList(1, currentPage, 10);
		 		
				for(int i=0; i<adminmainbean.length; i++){
					
						adminmainbean[i].setCheck(adminmaindao.checkGet("OLDPLUS", "Old", adminmainbean[i].getID()));
				}
				
				System.out.println("노인회원목록 모으자!!");
				request.setAttribute("page","/Manage/Man_OldPeople");
				request.setAttribute("adminmain", adminmainbean);
				request.setAttribute("paging", paging.getReturnHtml());
				view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
				view.forward(request, response);
			*/
			}else if(action_code.equals("delet")){
				
				int id = Integer.parseInt(request.getParameter("id"));
				String age = request.getParameter("age");
				int level = adminmaindao.failLevel(id);
				
					if(level == 1){
						adminmaindao.deletO(id);
					}else if(level == 2){
						adminmaindao.deletY(id);
					}
				out.println( "<script>" );
				out.println( "alert(\"削除処理が完了しました。\");");
				out.println( "location.href=\"/KOMORIUTA/AdminMem.do?action_code="+age+"list\";" );
				out.println( "</script>" );
				
			}else if(action_code.equals("business")){
				int youid;
				int id = Integer.parseInt(request.getParameter("id"));
				int level = adminmaindao.levelGet(id);

				if(level == 2){
					youid = adminmaindao.mapping(id, "Map_Young", "Map_Old");
				}else{
					youid = adminmaindao.mapping(id, "Map_Old", "Map_Young");
				}
				
				String result = adminmaindao.youNickGet(youid);

	 			String resultString = null;			

				resultString = "<response><result>"+result+"</result></response>";
			    PrintWriter out1 = response.getWriter();
			    response.setContentType("text/xml;charset=utf-8");
			   
			    out1.println(resultString);
			    out1.close();
				
			}else if(action_code.equals("mypage")){
				int id = Integer.parseInt(request.getParameter("id"));
System.out.println("ajax");				
				String result1 = adminmaindao.shareGet(id);
				boolean check = adminmaindao.blog(result1);
				String result2 = null;
				if(check){
					result2 = "yes";
				}else{
					result2 = "no";
				}
				 
				JSONArray inner = new JSONArray();
				JSONObject obj = new JSONObject();
				obj.put("share", result1);
				obj.put("val", result2);

				out.println(obj);
				out.close();
				
			}else if(action_code.equals("oldmypage")){
				String nick = request.getParameter("nick");
System.out.println("ajax2");				
				String result1 = adminmaindao.youNickGet(adminmaindao.mistressGet(nick));
				boolean check = adminmaindao.blog(result1);
				String result2 = null;
				if(check){
					result2 = "yes";
				}else{
					result2 = "no";
				}
								 
				JSONArray inner = new JSONArray();
				JSONObject obj = new JSONObject();
				obj.put("share", result1);
				obj.put("val", result2);

				out.println(obj);
				out.close();
							
			}
			
		}
}
